#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = "Administrator"
__name__="html_helper"
__date__="2016/9/7"
__time__="14:33"
"""
from django.utils.safestring import mark_safe

# 如果将此分页应用到其他web框架，需要修改mark_safe模块。
class PageInfo:
    '''
    用来计算
    接收: count，page,page_item=5
    产出: start,end,all_page_count
    '''
    def __init__(self,current_page,all_count,per_item=5):
        self.CurrentPage = current_page
        self.AllCount = all_count
        self.PerItem = per_item
    @property   # 特性---调用此方法十，不用加括号
    def start(self):
        return (self.CurrentPage-1)*self.PerItem
    @property
    def end(self):
        return self.CurrentPage * self.PerItem
    @property
    def all_page_count(self):
        temp = divmod(self.AllCount,self.PerItem)
        if temp[1] == 0:
            all_page_count = temp[0]
        else:
            all_page_count = temp[0] + 1
        return all_page_count

def Pager(page,all_page_count):
    '''
    :param page: 当前页
    :param all_page_count: 总页数
    :return 生成分页
    '''

    page_html=[]
    first_html = "<a href='/app03/index/%d'>首页</a>" %(1,)
    page_html.append(first_html)
    if page <=1:
        prev_html = "<a href='#'>上一页</a>"
    else:
        prev_html = "<a href='/app03/index/%d'>上一页</a>" %(page-1,)
    page_html.append(prev_html)

    if all_page_count < 11:
        begin = 0
        end = all_page_count
    else:
        if page < 6:
            begin = 0
            end = 11
        else:
            if page + 6 > all_page_count:
                begin = page - 6
                end = all_page_count
            else:
                begin = page - 6
                end = page + 5
    #for i in range(all_page_count):
    for i in range(begin,end):
        if page == i+1:
            #a_html = "<a style='color:red';href='/app03/index/%d'>%d</a>" %(i+1,i+1)    # 加红显示当前页
            a_html = "<a class='selected';href='/app03/index/%d'>%d</a>" %(i+1,i+1) # 应用样式
        else:
            a_html = "<a href='/app03/index/%d'>%d</a>" %(i+1,i+1)

        page_html.append(a_html)
    if page < all_page_count:
        next_html = "<a href='/app03/index/%d'>下一页</a>" %(page+1,)
    else:
        next_html= "<a href='#'>下一页</a>"
    page_html.append(next_html)
    end_html = "<a href='/app03/index/%d'>尾页</a>" %(all_page_count,)
    page_html.append(end_html)


    page_string = mark_safe(''.join(page_html))  #将html标签 拼接后渲染到后。
    return page_string